package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;
import androidx.recyclerview.widget.RecyclerView;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.ImageAdapter;
import com.csweimei.rentmanagementtreasure.http.entity.HouseDetailZzBean;
import com.csweimei.rentmanagementtreasure.pop.ImageShowPop;
import com.csweimei.rentmanagementtreasure.pop.PopUtils;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.viewModel.TitleVM;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.utils.RxUtils;

/**
 * 整租房屋详情
 */
public class EntHouseDetailVM extends TitleVM {
    public ObservableField<HouseDetailZzBean.DataDTO.HouserDataDTO> houseInfo = new ObservableField<>();

    public ObservableField<HouseDetailZzBean.DataDTO.LeaseListDTO> leaseInfo = new ObservableField<>();

    public EntHouseDetailVM(@NonNull Application application, BaseModel model) {
        super(application, model);
        setTitleText("房源详情");
    }

    public ObservableField<String> houseId = new ObservableField<>();
    public ObservableField<String> type = new ObservableField<>();

    public ObservableField<Integer> showLease = new ObservableField<>(View.GONE);

    @Override
    public void onCreate() {
        super.onCreate();
        getZzHouseData();
    }

    /**
     * 获取整租房源数据
     */
    private void getZzHouseData() {
        Map<String,String> map = new HashMap<>();
        map.put("sid",houseId.get());
        model.allhouser_data(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<HouseDetailZzBean>() {
                    @Override
                    public void onNext(HouseDetailZzBean o) {
                        if (o.issucc){
                            if (ValidateUtils.isNotEmptyObjectOrString(o.getData().getHouserData())){
                                houseInfo.set(o.getData().getHouserData());
                                setImgRecy(houseInfo.get().getImgsrc());
                            }

                            if (ValidateUtils.isNotEmptyObjectOrString(o.getData().getLeaseList())){
                                if (ValidateUtils.isNotEmptyString(o.getData().getLeaseList().getName())){
                                    leaseInfo.set(o.getData().getLeaseList());
                                    showLease.set(View.VISIBLE);
                                }else {
                                    showLease.set(View.GONE);
                                }

                            }

                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void setImgRecy(String imgsrc) {
        List<String> list = new ArrayList<>();
        if (imgsrc.contains(",")){
            list.addAll(Arrays.asList(imgsrc.split(",")));
        }else {
            list.add(imgsrc);
        }
        ImageAdapter imageAdapter = new ImageAdapter();
        imageAdapter.setList(list);
        imageAdapter.addChildClickViewIds(R.id.img);
        imageAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            showImgPop(imageAdapter.getData().get(position));
        });
        recyclerView.setAdapter(imageAdapter);

    }

    private void showImgPop(String s) {
        ImageShowPop imageShowPop = new ImageShowPop(mActivity,s);
        PopUtils.showPop(mActivity,imageShowPop,false);
    }

    RecyclerView recyclerView;
    public void setRecy(RecyclerView imgRecy) {
        recyclerView = imgRecy;
    }
}
